package com.ksyun.campus.client;


import com.ksyun.campus.client.domain.ClusterInfo;
import com.ksyun.campus.client.domain.StatInfo;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        // 创建系统
        EFileSystem eFileSystem=new EFileSystem("/D:");
        // 创建文件并向其写入数据
//        createAndWrite(eFileSystem);
//
//        // 打开文件并读取文件信息后写入另一个文件
//        read(eFileSystem);
//
//        // 获取集群信息
//        ClusterInfo clusterInfo = eFileSystem.getClusterInfo();
//        System.out.println(clusterInfo);
//
//        // 创建目录
//        boolean mkdir = eFileSystem.mkdir("/kingsoft/test/directory");
//        System.out.println(mkdir);
//
//        //获取文件状态
//        StatInfo fileStats = eFileSystem.getFileStats("/kingsoft/111.txt");
//        System.out.println(fileStats);
//
//        // 获取文件夹下的所以文件的状态
//        List<StatInfo> statInfos = eFileSystem.listFileStats("/kingsoft");
//        System.out.println(statInfos);

        // 删除文件
        delete(eFileSystem);
    }

    public static void createAndWrite(EFileSystem eFileSystem){
        FSOutputStream fsOutputStream = null;
        FSOutputStream fsOutputStream1 = null;
        FSOutputStream fsOutputStream2 = null;
        try {
            fsOutputStream = eFileSystem.create("/kingsoft/111.txt");
            fsOutputStream.write("11111".getBytes(StandardCharsets.UTF_8));

            fsOutputStream1 = eFileSystem.create("/kingsoft/write2.txt");
            fsOutputStream1.write("222222".getBytes(StandardCharsets.UTF_8));

            fsOutputStream2 = eFileSystem.create("/kingsoft/write3.txt");
            fsOutputStream2.write("3333".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fsOutputStream.close();
                fsOutputStream1.close();
                fsOutputStream2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void read(EFileSystem eFileSystem){
        FSInputStream open = eFileSystem.open("/kingsoft/111.txt");
        try {
            byte[] data=new byte[10];
            int read = open.read(data);
            FSOutputStream fsOutputStream = eFileSystem.create("/kingsoft/222.txt");
            fsOutputStream.write(data,0,read);
            String s = new String(data);
            System.out.println(s);
            System.out.println(read);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void delete(EFileSystem eFileSystem){
        eFileSystem.delete("/kingsoft/111.txt");
        eFileSystem.delete("/kingsoft/222.txt");
        eFileSystem.delete("/kingsoft/write2.txt");
        eFileSystem.delete("/kingsoft/write3.txt");

    }
}
